package com.adcc.ags.agent.resouce;

import com.adcc.utility.log.Log;
import com.google.common.base.Optional;
import com.google.common.base.Strings;

import javax.ws.rs.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

/**
 * 网络监测服务
 */
@Path("/NetWorkCheck")
@Produces("application/json;charset=UTF-8")
public class NetWorkCheckResource {

    // ping命令
    private String ping;

    // tracert命令
    private String tracert;

    /**
     * 构造函数
     * @param ping
     * @param tracert
     */
    public NetWorkCheckResource(String ping,String tracert){
        this.ping = ping;
        this.tracert = tracert;
    }

    @POST
    @Path("/ping")
    public Optional<String> ping(@FormParam("ip") String ip,@FormParam("count") String conut){
        BufferedReader br = null;
        try{
            if(Strings.isNullOrEmpty(ip) || Strings.isNullOrEmpty(conut)){
                return Optional.absent();
            }
            String strLine = null;
            StringBuilder sb = new StringBuilder();
            String strPing = String.format(ping,ip,conut);
            Process process = Runtime.getRuntime().exec(strPing);
            br = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));
            while ((strLine = br.readLine()) != null){
                sb.append(strLine);
                sb.append("\r\n");
            }
            return Optional.of(sb.toString());
        }catch (Exception ex){
            Log.error(NetWorkCheckResource.class.getName(),"ping() error",ex);
            return Optional.absent();
        }finally {
            if(br != null){
                try {
                    br.close();
                } catch (IOException ex) {
                    Log.error(NetWorkCheckResource.class.getName(),"close IO error",ex);
                }
            }
        }
    }

    @POST
    @Path("/tracert")
    public Optional<String> tracert(@FormParam("ip") String ip){
        BufferedReader br = null;
        try{
            if(Strings.isNullOrEmpty(ip)){
                return Optional.absent();
            }
            String strLine = null;
            StringBuilder sb = new StringBuilder();
            String strTracert = String.format(tracert,ip);
            Process process = Runtime.getRuntime().exec(strTracert);
            br = new BufferedReader(new InputStreamReader(process.getInputStream(),Charset.forName("GBK")));
            while ((strLine = br.readLine()) != null){
                sb.append(strLine);
                sb.append("\r\n");
            }
            return Optional.of(sb.toString());
        }catch (Exception ex){
            Log.error(NetWorkCheckResource.class.getName(),"tracert() error",ex);
            return Optional.absent();
        }finally {
            if(br != null){
                try {
                    br.close();
                } catch (IOException ex) {
                    Log.error(NetWorkCheckResource.class.getName(),"close IO error",ex);
                }
            }
        }
    }
}